<?
//<@
import ("stock_jsml_JsmlConfig");
//@>
class jsml_View extends jsml_Bean {
	//<@
	public $ds;
	//@>
	public $div;
	public $vo;
	public $screenW;
	public $screenH;
	public function construct($key) {
		//<@
		parent::construct($key);
		//if (null == $key) {
			$jsmlConfig = stock_jsml_JsmlConfig::$config;
			$this->ds = $jsmlConfig->getBean($jsmlConfig->Ds)->pdo;
			$clazz = get_class($this);
			$clazz = substr($clazz, 0, -4) . "Vo";
			if(class_exists($clazz)) $this->vo = new $clazz;
		//}
		return $this;
		//@>

		parent::construct($key);
		//if (null == $key) {
			$this->screenW = $_(window)->width();
			$this->screenH = $_(window)->height();
			$this->div = _("<div></div>");
			if($this->config!=null) $this->div->css($this->config->getCss($this->__class->__name));
			else $this->div->css($this->getCss($this->__class->__name));
			$clazz = $this->__class->__className;
			$clazz = substr($clazz, 0, $clazz->length-4) . "Vo";
			try{
				eval(implode("", array('$this.vo = new ', $clazz,"();")));
			} catch(Exception $ex){}
		//}
		return $this;
	}
	protected function configCss($key) {
		$css = array(
			"position" => "absolute",
			//"overflow" => "hidden",
			"left" => "0px",
			"top" => "0px",
			"width" => "0px",
			"height" => "0px"
			//"border-width" => 0
		);
		if (null == $key) {
		}
		return $css;
	}
	public function getCss($key, $cssExtra) {
		$css = $this->configCss($key);
		if($cssExtra != null) $_->extend($css, $cssExtra);
		return $css;
	}

	public function getCssString($key, $cssExtra) {
		$css = $this->configCss($key);
		if($cssExtra != null) $_->extend($css, $cssExtra);
		$cssString = array();
		foreach($css as $key=>$value){
			array_push($cssString, $key, ":", $value, ";");
		}
		return implode("", $cssString);
	}

	protected $paramArrayMap = array("__new__"=>1);
	public function jsonp(){
		$func = $this->__class->__getReflection()->getFuncName($this->jsonp->caller);
		$paramArray = array();
		for( $i=0; $i<$__arguments->length; $i++){
			$paramArray->push($__arguments[$i]);
		}

		$id = $this->getId();
		$this->paramArrayMap[$id] = $paramArray;
		$_->ajax(array(
		url=> implode("",array("index.jsml.php?","bean=",$this->config->__class->__className, "/", $this->__class->__name,"&function=",$func, "&__id__=", $id)),
		data=> implode("",array("paramArray=", JSON::stringify($paramArray))),
		dataType=> "jsonp",
		success=> function($result, $textStatus){
			//try{
				$id = $result["__id__"];
				$result["paramArray"] = $this->paramArrayMap[$id];
				$this->paramArrayMap[$id] = null;
				_($this)->trigger("rpc.".$func, $result);
			//} catch(Exception $ex){
				//$__console->error($ex);
				//$__console->trace();
			//}
		},
		error=> function ($XMLHttpRequest, $textStatus, $errorThrown) {
			//$__console->error($errorThrown);
			throw $errorThrown;
		}
		));
	}
	public function jsonPost(){
		$func = $this->__class->__getReflection()->getFuncName($this->jsonPost->caller);
		$paramArray = array();
		for( $i=0; $i<$__arguments->length; $i++){
			$paramArray->push($__arguments[$i]);
		}

		$id = $this->getId();
		$this->paramArrayMap[$id] = $paramArray;
		$_->ajax(array(
		url=> implode("",array("index.jsml.php?","bean=",$this->config->__class->__className, "/", $this->__class->__name,"&function=",$func, "&__id__=", $id)),
		data=> JSON::stringify($paramArray),
		dataType=> "json",
		type=> "POST",
		contentType=> "application/json",
		success=> function($result, $textStatus){
			//try{
				$id = $result["__id__"];
				$result["paramArray"] = $this->paramArrayMap[$id];
				$this->paramArrayMap[$id] = null;
				_($this)->trigger("rpc.".$func, $result);
			//} catch(Exception $ex){
				//$__console->error($ex);
			//}
		},
		error=> function ($XMLHttpRequest, $textStatus, $errorThrown) {
			//$__console->error($errorThrown);
			throw $errorThrown;
		}
		));
	}
	public static $id = 0;
	public function getId(){
		return implode("", array("__",jsml_View::$id++,"__"));
	}
	public $mapId = array("__new__"=>1);
	public function attach($key){
		$id = $this->getId();
		$this->mapId[$key] = $id;
		return implode("", array("id='",$id,"'"));
	}
	public $map_ = array("__new__"=>1);
	public function _($key){
		if($this->map_[$key]!=null) return $this->map_[$key];
		$id = $this->mapId[$key];
		if($id==null) return null;
		$this->map_[$key] = $this->div->find(implode("", array("#",$id)));
		return $this->map_[$key]; 
	}
	public static $zindex = 1000;
	public function toFront($key){
		$this->_($key)->css("z-index", ++jsml_View::$zindex);
	}
	
	public function loadJson($file){
		if(file_exists($file)){
			return json_decode(file_get_contents($file), TRUE);
		}
		return null;
	}
	public function saveJson($file, $obj){
		file_put_contents($file, json_encode($obj));
	}
}
